package com.goku.calculator;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * 计算器界面
 * @author goku
 */
public class MainActivity extends Activity implements OnTouchListener {

    /**
     * 小数点后有效数字
     */
    private static final int LENGTH = 11;
    /**
     * 最大数字长度
     */
    private static final int LENGTHSCALE = 12;
    /**
     * 计算器显示器
     */
    private EditText editText;
    /**
     * 是否为第一次输入数字
     */
    private boolean isFirstInput;
    /**
     * 当前显示的操作数
     */
    private String operatorNumber;
    /**
     * 点击的按钮
     */
    private Button clickButton;
    /**
     * 操作符
     */
    private String operator;
    /**
     * 记录的上一次操作数
     */
    private String firstNumber;
    /**
     * 数字是否达到最大长度
     */
    private boolean isFull;
    /**
     * 计算结果
     */
    private BigDecimal result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.et_view);
        editText.setOnTouchListener(this);
        clear();
    }

    /**
     * 按钮监听器
     * @param view
     *            点击的按钮控件
     */
    public void OnButtonListener(View view) {
        clickButton = (Button) view;
        switch (clickButton.getId()) {
            case R.id.bt_clear:
                clear();
                break;
            case R.id.bt_sign:
                if (editText.getText().toString().equals("error")) {
                    break;
                }
                negate();
                break;
            case R.id.bt_del:
                if (editText.getText().toString().equals("error")) {
                    break;
                }
                delete();
                break;
            case (R.id.bt_minus):
            case (R.id.bt_add):
            case (R.id.bt_divide):
            case (R.id.bt_multiply):
            case (R.id.bt_result):
                if (editText.getText().toString().equals("error")) {
                    break;
                }
                calculate();
                break;
            default:
                if (editText.getText().toString().equals("error")) {
                    break;
                }
                input();
                break;
        }
    }

    /**
     * 清空按钮
     */
    public void clear() {
        isFull = false;
        isFirstInput = true;
        editText.setText("0");
        Editable ea = editText.getText();
        editText.setSelection(ea.length());
        operatorNumber = "0";
        operator = "";
    }

    /**
     * 删除按钮
     */
    public void delete() {
        String editString = editText.getText().toString();
        if (editString.length() == 1) {
            clear();
        } else {
            editText.setText(editString.subSequence(0, editString.length() - 1));
            Editable ea = editText.getText();
            editText.setSelection(ea.length());
        }
        operatorNumber = editText.getText().toString();
    }

    /**
     * 输入数字及小数点函数
     */
    public void input() {
        if (isFirstInput) {
            String text = clickButton.getText().toString();
            if (text == ".") {
                text = "0" + text;
            }
            editText.setText(text);
            isFirstInput = false;
        } else {
            isFull = editText.length() >= LENGTHSCALE;
            if (!isFull) {
                editText.append(clickButton.getText().toString());
            }
        }
        Editable ea = editText.getText();
        editText.setSelection(ea.length());
        operatorNumber = editText.getText().toString();
        if (operatorNumber.equals(".")) {
            operatorNumber = "0";
        }
        if (operator.equals("=")) {
            result = new BigDecimal(operatorNumber);
        }
    }

    /**
     * 计算函数
     */
    public void calculate() {
        if (operator.equals("+")) {
            add();
        } else if (operator.equals("-")) {
            minus();
        } else if (operator.equals("*")) {
            mutiply();
        } else if (operator.equals("/")) {
            div();
        } else if (operator.equals("")) {
            if (operatorNumber.equals(".")) {
                operatorNumber = "0";
            }
            result = new BigDecimal(operatorNumber);
        }
        if (!editText.getText().toString().equals("error")) {
            isFirstInput = true;
            operator = clickButton.getText().toString();
            if (String.valueOf(result).length() > LENGTHSCALE) {
                DecimalFormat f = new DecimalFormat();
                f.applyPattern("#.##E000");
                editText.setText(f.format(result));
            } else {
                editText.setText(String.valueOf(result));
            }
            Editable ea = editText.getText();
            editText.setSelection(ea.length());
            firstNumber = editText.getText().toString();
        }
    }

    /**
     * 加法
     */
    public void add() {
        BigDecimal a = new BigDecimal(firstNumber);
        BigDecimal b = new BigDecimal(operatorNumber);
        result = a.add(b);
    }

    /**
     * 减法
     */
    public void minus() {
        BigDecimal a = new BigDecimal(firstNumber);
        BigDecimal b = new BigDecimal(operatorNumber);
        result = a.subtract(b);
    }

    /**
     * 乘法
     */
    public void mutiply() {
        BigDecimal a = new BigDecimal(firstNumber);
        BigDecimal b = new BigDecimal(operatorNumber);
        result = a.multiply(b);
    }

    /**
     * 除法
     */
    public void div() {
        if (operatorNumber.equals("0")) {
            editText.setText("error");
            Editable ea = editText.getText();
            editText.setSelection(ea.length());
        } else {
            BigDecimal a = new BigDecimal(firstNumber);
            BigDecimal b = new BigDecimal(operatorNumber);
            try {
                result = a.divide(b);
            } catch (Exception e) {
                result = a.divide(b, LENGTH, BigDecimal.ROUND_HALF_UP);
            }

        }
    }

    /**
     * 取反
     */
    public void negate() {
        if (!editText.getText().toString().equals(".")) {
            BigDecimal a = new BigDecimal(editText.getText().toString());
            editText.setText(String.valueOf(a.negate()));
            Editable ea = editText.getText();
            editText.setSelection(ea.length());
            operatorNumber = editText.getText().toString();
            operator = "";
        }
    }

    /**
     * 点击计算器显示器
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        // editText.setInputType(InputType.TYPE_NULL);
        v.requestFocus();
        return true;
    }

}
